<?php
/*读者墙实现
作者:rakiy
时间:2011-11-20
说明:$num为条数
*/
function readers($num=10,$width=32,$height=32,$prefix=''){
  $period = 25920000; // 時段: 7 天, 單位: 秒
  $path = __TYPECHO_ROOT_DIR__ .'/usr/uploads/cache/'; //缓存路径
  $file =  $path . $prefix . 'y2_readers.php';
  if (!file_exists($file) || time() - filemtime($file) > $period){
    $counts = Typecho_Db::get()->fetchAll(Typecho_Db::get()
    ->select('COUNT(author) AS cnt','author', 'url', 'mail')
    ->from('table.comments')
    ->where('created > ?', $period )
    ->where('status = ?', 'approved')
    ->where('type = ?', 'comment')
    ->where('authorId = ?', '0')
    ->group('author')
    ->order('cnt', Typecho_Db::SORT_DESC)
    ->limit($num)
    );
    SaveCache($file,serialize($counts));
  }else{
    $counts =  unserialize(LoadCache($file));
    if($counts) exit('暂无数据');
  }
  $mostactive = '';
  $avatar_path = Helper::options()->siteUrl . 'usr/uploads/avatar/';
  foreach ($counts as $count) {
    $avatar = $avatar_path . md5(strtolower($count['mail'])) . '.jpg';
    $c_url = isset($count['url'])?$count['url']:'#;';
    if ($c_url == '#;') {$cn_url = '还没有小窝...';}else{$cn_url = $c_url;} 
    $mostactive .= "<li class='avatar'><a href='" . $c_url . "' title='" . $count['author'] . " (" . $count['cnt'] . "条评论)' target='_blank' rel='nofollow'><img src='" . $avatar . "' alt='' width='".$width."px' height='".$height."px'/></a></li>\n";
  }
  echo $mostactive;
}
//公共过程 2012-12-07
//缓存读写
function SaveCache($file,$str){
  $fp = fopen($file, "wb");
  fwrite($fp, $str);
  fclose($fp);
}
function LoadCache($file){
  if(!file_exists($file)){
    return false;
  }
  return file_get_contents($file);
}
/*浏览器及操作系统简单判断
作者:rakiy
时间:2011-12-01
说明:$agent是从库里读出的数据
*/
function GetBrowser($agent)
{
      if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs))
      {
          $outputer = 'Internet Explorer' . ' ' . $regs[1];
      }
      else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs))
      {
          $outputer = 'Mozilla FireFox ' . ' ' .  $regs[1];
      }  
      else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs))
      {
          $outputer='Google Chrome' . ' ' .  $regs[2];
      }  
      else if (preg_match('/QQBrowser\/([^\s]+)/i', $agent, $regs))
      {
          $regg = explode("/",$regs[1]);
          $outputer = 'QQ浏览器' . ' ' .  $regg[0] ;
      }
      else if (preg_match('/UC/i', $agent))
      {
          $outputer = 'UCWeb' . ' ' . '8.11112510';
      }
      else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs))
      {
          $outputer = 'Apple Safari' . ' ' .  $regs[1];
      }
      else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs))
      {
          $outputer = 'Opera' . ' ' .  $regs[1];
      }
      else
      {
          $outputer = '其它浏览器';
      }
echo $outputer;
}
 //开始判断操作系统
function GetOs($agent)
{
	$os = false;
  	if (preg_match('/win/i', $agent))
        {
           
            if(preg_match('/nt 6.1/i', $agent))
            {
                $os = 'Windows 7';
            }
            else if(preg_match('/nt 5.1/i', $agent))
            {
            	$os = 'Windows XP';       
            }            
           	else if(preg_match('/nt 6.2/i', $agent))
          	 {
             			$os = 'Windows 8';
            }
           	else if(preg_match('/nt 6.0/i', $agent))
            {
                $os = 'Windows Vista';
            }         
            else if(preg_match('/nt 5/i', $agent))
            {
                 $os = 'Windows 2000';
            }
             else{
                  $os = 'Windows';
            }
        }
     else if(preg_match('/android/i', $agent))
        {
             $os = 'Android';
        } 
    else if (preg_match('/ubuntu/i', $agent))
        {
             $os = 'Ubuntu';
        }
	else if (preg_match('/linux/i', $agent))
        {
             $os = 'Linux';
        }
     else if (preg_match('/mac/i', $agent))
        {
            $os = 'Mac OS X';
        }     
     else if (preg_match('/unix/i', $agent))
        {
            $os = 'Unix';
        }
     else if (preg_match('/symbian/i', $agent))
        {
            $os = 'Nokia SymbianOS';
        }        
     else
        {
            $os = '其它操作系统';
        }
	 echo $os;
}
function quote_change($str,$filter=0){
	$pat = '/\[quote(.*?)id=([0-9]*)\](.*?)\[\/quote\]/i';
	if ($filter){
		$str=preg_replace($pat , '' , $str);
	}else{
		$str=preg_replace($pat,'<a href="#comment-$2 ">@$3</a>', $str);	
	}
	return $str;
}
function tags($num=20){

$counts = Typecho_Db::get()->fetchAll(Typecho_Db::get()
->select('name','count')
->from('table.metas')
->where('type = ?', 'tag')
->order('count', Typecho_Db::SORT_DESC)
->limit($num)
);
$alltags='';
$tag_path = Helper::options()->siteUrl . 'tag/';
foreach ($counts as $count) {
	$alltags .= "<a style='color:rgb(".mt_rand(0,255).",".mt_rand(0,255).",".mt_rand(0,255).")' href='" . $tag_path . $count['name'] . "' title='有".$count['count']."篇文章使用".$count['name']."标签'>".$count['name']."</a>\n";
}
echo $alltags;
}
//读取管理员评论数量
function Count_v($cid,$commentsNum,$type = null){
	$count_v = Typecho_Db::get()->fetchObject(Typecho_Db::get()->select(array('COUNT(coid)' => 'num'))->from('table.comments')->where('table.comments.status = ?', 'approved')->where('table.comments.cid = ?', $cid)
		->where('table.comments.authorId = ?', '1')
		->where('table.comments.type = ?', 'comment')
		)->num;
	if ($type){
		$count = ($commentsNum - $count_v)?($commentsNum - $count_v.'条评论'):('我来评论');
		echo $count;
	}else{
		return $count_v ;	
	}	
}
?>